package com.foreverwin.mes.lims.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mes.common.enums.IDUtils;
import com.foreverwin.mes.common.service.MessageSendService;
import com.foreverwin.mes.core.base.FrontPage;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.lims.mapper.CommandReviewMapper;
import com.foreverwin.mes.lims.model.CommandReview;
import com.foreverwin.mes.lims.model.CommandSend;
import com.foreverwin.mes.lims.mapper.CommandSendMapper;
import com.foreverwin.mes.lims.service.CommandSendService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.foreverwin.mes.lims.service.ProducerService;
import com.foreverwin.mes.lims.util.DataValueConstant;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * <p>
 * 指令下达 服务实现类
 * </p>
 *
 * @author Max
 * @since 2020-02-27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CommandSendServiceImpl extends ServiceImpl<CommandSendMapper, CommandSend> implements CommandSendService {


    @Autowired
    private CommandSendMapper commandSendMapper;
    @Autowired
    private CommandReviewMapper commandReviewMapper;
    @Autowired
    MessageSendService messageSendService;



    @Override
    public Page<CommandSend> selectPage(FrontPage<CommandSend> frontPage, CommandSend commandSend) {
        EntityWrapper<CommandSend> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(commandSend);
        return super.selectPage(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<CommandSend> selectList(CommandSend commandSend) {
        EntityWrapper<CommandSend> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(commandSend);
        return super.selectList(queryWrapper);
    }

    @Override
    public int saveAndSendCommand(CommandSend commandSend) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        commandSend.setHandle( IDUtils.createID() );
        commandSend.setSite(site);
        commandSend.setMessageId( "eq.holdRelease" );
        commandSend.setReqId( "REQ_"+IDUtils.createID() );
        commandSend.setSendTime(new Date());
        commandSend.setUserId( user );
        commandSend.setCreator( user );
        commandSend.setUpdater( user );
        commandSend.setCreatedDateTime( new Date() );
        commandSend.setModifiedDateTime( new Date() );

        int count = commandSendMapper.insert(commandSend);
        CommandReview commandReview = new CommandReview();
        if (DataValueConstant.RESET_MACHINE.equals( commandSend.getMode() )) {//复机指令先审核

        commandReview.setHandle( IDUtils.createID() );
        commandReview.setzCommandSendBo( commandSend.getHandle() );
        commandReview.setSite( site );
        commandReview.setResrce( commandSend.getResrce() );
        commandReview.setResrceDesc( commandSend.getResrceDesc() );
        commandReview.setApplyType( "复机申请" );
        commandReview.setEvent( "复机" );
        commandReview.setMessage( commandSend.getMessage() );
        commandReview.setSubmitter( commandSend.getUserId() );
        commandReview.setSubmitTime( commandSend.getSendTime() );
        commandReview.setCreatedDateTime( new Date() );
        commandReview.setModifiedDateTime( new Date() );
        commandReviewMapper.insert( commandReview );
        } else if (DataValueConstant.STOP_MACHINE.equals( commandSend.getMode() )) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("MESSAGE_ID",commandSend.getMessageId() );
            jsonObject.put( "REQ_ID", commandSend.getReqId() );
            jsonObject.put("SITE",site);
            jsonObject.put("FACTORY",commandSend.getFactory() );
            jsonObject.put( "LINE_ID",commandSend.getLineId() );
            jsonObject.put("RESOURCE",commandSend.getResrce() );
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            jsonObject.put("SEND_TIME",simpleDateFormat.format( new Date() ));
            jsonObject.put("MODE",commandSend.getMode());
            jsonObject.put("CODE",commandSend.getCode());
            jsonObject.put("USER_ID",user);
            messageSendService.send( "eq.holdRelease", jsonObject );
            /*JSONObject _jsonObject = messageSendService.sendAndReceive( "eq.holdRelease", jsonObject);
            if (_jsonObject != null ) {
                CommandSend cs = new CommandSend();
                cs.setReqId( _jsonObject.getString("REQ_ID"));
                CommandSend _cs =commandSendMapper.selectOne(cs);
                if ( _cs != null ) {
                    _cs.setResult(_jsonObject.getString("RESULT"));
                    _cs.setMessage(_jsonObject.getString("MESSAGE"));
                    _cs.setModifiedDateTime(new Date());
                    commandSendMapper.updateById(_cs);
                }
            }*/
        } else {
            throw BusinessException.build("指令异常!");
        }
        return count;
    }


}